from _collections import deque
class BiTreeNode:
    def __init__(self, data):
        self.data = data
        self.lchild = None
        self.rchild = None
        self.parent = None

class BST:
    def __init__(self, li=None):
        self.root = None
        if li:
            for val in li:
                self.insert_no_rec(val)

    def is_empty(self):
        return self.root == None

    def search(self, node, val):
        if not node:
            return None
        else:
            if val < node.data:
                return self.search(node.lchild,val)
            elif val > node.data:
                return self.search(node.rchild,val)
            else:
                return node

    def search_no_rec(self, val):
        p = self.root
        while p:
            if val < p.data:
                p = p.lchild
            elif val > p.data:
                p = p.rchild
            else:
                return p
        return None

    def delete(self, val):
        def remove_node_1(node):  #删除节点为leaf
            if not node.parent:
                self.root = None
            if node == node.parent.lchild:
                node.parent.lchild = None
                node.parent = None
            elif node == node.parent.rchild:
                node.parent.rchild = None
                node.parent = None

        def remove_node_21(node):  # node只有左子节点
            if not node.parent:
                self.root = node.lchild
                node.lchild.parent = None
            elif node == node.parent.lchild:
                node.parent.lchild = node.lchild
                node.lchild.parent = node.parent
            elif node == node.parent.rchild:
                node.parent.rchild = node.lchild
                node.lchild.parent = node.parent

        def remove_node_22(node):  # node是有右子节点
            if not node.parent:
                self.root = node.rchild
                node.rchild.parent = None
            elif node == node.parent.lchild:
                node.parent.lchild = node.rchild
                node.rchild.parent = node.parent
            elif node == node.parent.rchild:
                node.parent.rchild = node.rchild
                node.rchild.parent = node.parent

        if self.root:  # 不是空树
            node = self.search_no_rec(val)
            if not node:
                return False
            if not node.lchild and not node.rchild:  # leaf节点
                remove_node_1(node)
            elif not node.rchild:  # 只有左子节点
                remove_node_21(node)
            elif not node.lchild:  # 只有右子节点
                remove_node_22(node)
            else:  # 有两个子节点
                min_node = node.rchild
                while min_node.lchild:
                    min_node = min_node.lchild
                node.data = min_node.data
                if min_node.rchild:
                    remove_node_22(min_node)
                else:
                    remove_node_1(min_node)

    def insert(self, node, val):
        if not node:
            node = BiTreeNode(val)
        elif val < node.data:
            node.lchild = self.insert(node.lchild, val)
            node.lchild.parent = node
        elif val > node.data:
            node.rchild = self.insert(node.rchild, val)
            node.rchild.parent = node
        return node

    def insert_no_rec(self, val):
        p = self.root
        if not p:
            self.root = BiTreeNode(val)
            return
        while True:
            if val < p.data:
                if p.lchild:
                    p = p.lchild
                else:
                    p.lchild = BiTreeNode(val)
                    p.lchild.parent = p
                    return
            elif val > p.data:
                if p.rchild:
                    p = p.rchild
                else:
                    p.rchild = BiTreeNode(val)
                    p.rchild.parent = p
                    return
            else:
                return

    def pre_order(self,root):
        if root:
            print(root.data, end=',')
            self.pre_order(root.lchild)
            self.pre_order(root.rchild)

    def in_order(self, root):
        if root:
            self.in_order(root.lchild)
            print(root.data, end=',')
            self.in_order(root.rchild)

    def post_order(self, root):
        if root:
            self.post_order(root.lchild)
            self.post_order(root.rchild)
            print(root.data, end=',')

    def level_order(self,root):
        queue = deque()
        queue.append(root)
        while len(queue) > 0:
            node = queue.popleft()
            print(node.data, end=',')
            if node.lchild:
                queue.append(node.lchild)
            if node.rchild:
                queue.append(node.rchild)

    def max_node(self):
        node = self.root
        while node.rchild:
            node = node.rchild
        return node

    def min_node(self):
        node = self.root
        while node.lchild:
            node = node.lchild
        return node

# tree = BST([4,6,7,9,2,1,3,5,8,10])
#
# print(tree.is_empty())
#
# tree.in_order(tree.root)
# print()
# tree.pre_order(tree.root)
# print()
# tree.level_order(tree.root)
# print()
# tree.level_order(tree.root)
# print()
# print(tree.min_node().data)
#
import networkx as nx
import matplotlib.pyplot as plt

def create_graph(G, node, pos={}, x=0, y=0, layer=1):
    pos[node.data] = (x, y)
    if node.lchild:
        G.add_edge(node.data, node.lchild.data)
        l_x, l_y = x - 1 / 2 ** layer, y - 1
        l_layer = layer + 1
        create_graph(G, node.lchild, x=l_x, y=l_y, pos=pos, layer=l_layer)
    if node.rchild:
        G.add_edge(node.data, node.rchild.data)
        r_x, r_y = x + 1 / 2 ** layer, y - 1
        r_layer = layer + 1
        create_graph(G, node.rchild, x=r_x, y=r_y, pos=pos, layer=r_layer)
    return (G, pos)

def draw(node):   # 以某个节点为根画图
    graph = nx.DiGraph()
    graph, pos = create_graph(graph, node)
    fig = plt.figure()
    ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
    # fig, ax = plt.subplots(figsize=(8, 10))  # 比例可以根据树的深度适当调节
    nx.draw_networkx(graph, pos, ax=ax, node_size=300)
    plt.show()

# tree = BST([1,3,63,2])
# draw(tree.root)
# tree.delete(2)
# draw(tree.root)